package com.jdiv.samples.arkanoid;

import java.awt.Point;

import com.jdiv.JDiv;
import com.jdiv.JProcess;

public class Ship extends JProcess {

	private int vel=15;
	
	private int w1;

	private int w2;

	private int w3;
	private Point v1;

	private Point v2;

	private Point v3;

	public Ship(){
		super();
		graph=3;
		x=(800/2);
		y= 600-50;
		
		width=106;
		
		w1=30;
		w2=46;
		w3=30;
		v1 = new Point(w1/2,y+(18/2));
		v2 = new Point(width/2,y+(18/2));
		v3 = new Point((w1+w2)+(w3/2),y+(18/2));
		
	}
	@Override
	public void begin() {}

	public void loop(){
		if (key(_LEFT)) x-=vel;
		if (key(_RIGHT)) x+=vel;
		
		
		if (!Main.ball.start){
			
			if (key(_SPACE)){
				Main.ball.start=true;
			}
			
			Main.ball.x=x;
			Main.ball.y=y-18;
		}else{
			if (collision(Main.ball)){
				
				int ang=0;
				//w_1 cuadrante 1, y asi sucesivamente
				//v_1 vector (el centro) de cada cuadrante
				if ( Main.ball.x >= (this.x - (width/2))  && Main.ball.x <= (this.x- (width/2))  +w1 ) {
				 ang=Main.ball.x*(v1.x+x) + Main.ball.y * v1.y ;
				}

				if ( Main.ball.x >= (this.x- (width/2))  + w1 && Main.ball.x <= (this.x- (width/2))+ (w1+w2) ) {
					 ang=Main.ball.x*(v2.x+x) + Main.ball.y * v2.y ;
					}
				
				if ( Main.ball.x >= (this.x- (width/2))  + (w1+w2) && Main.ball.x <=( this.x- (width/2) ) +width ) {
					 ang=Main.ball.x*(v3.x+x) + Main.ball.y * v3.y ;
					}
			
			//	int ang = - (JDiv.fget_angle(x,y,Main.ball.x, Main.ball.y));
				ang = (ang < 0) ? (ang % 360) + 360: ang % 360;
				  
				Main.ball.velX = (int)(((float) Main.ball.vel) * Math.cos(Math.toRadians(ang)) );
				Main.ball.velY = (int)(((float) Main.ball.vel) * Math.sin(Math.toRadians(ang)) );
			//	Main.ball.velY *=-1;
			
			 
				
			}
		}
		
	
	}
}
